/*
 * Copyright 2025 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

import ".."
import "../configitem"
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Window 2.15
import org.ukui.quick.items 1.0 as UkuiItems
import org.ukui.quick.platform 1.0

Window {
    id: window

    property int dragAreaHeight: 40
    property int itemHeight: 36
    property int dragX: 0
    property int dragY: 0
    property bool dragging: false
    property bool enableBackground: false

    function cancelAction() {
        window.destroy();
    }

    function saveAction() {
        if (layoutComboBox.currentIndex >= 0 && variantComboBox.currentIndex >= 0) {
            var layout = layoutManager.layout(layoutComboBox.currentIndex, variantComboBox.currentIndex);
            var imName = "keyboard-%0".arg(layout);
            // console.log("onLayoutChanged，layout：" + layout + ", imName:" + imName + ",current key:" + layoutManager.imEntries[0].key);
            layoutManager.defaultLayout = layout;
            if (layoutManager.imEntries.length === 0 || imName !== layoutManager.imEntries[0].key) {
                imContent.imName = imName;
                imContent.showWarning = true;
            }
        }
        window.destroy();
    }

    function handleDragStarted(posX, posY) {
        dragX = posX;
        dragY = posY;
        dragging = true;
    }

    function handleDragging(posX, posY) {
        if (dragging) {
            window.x += posX - dragX;
            window.y += posY - dragY;
        }
    }

    function handleCloseClicked() {
        window.destroy();
    }

    function handleDragEnd() {
        dragging = false;
    }

    width: 480
    height: 300
    modality: Qt.ApplicationModal
    flags: Qt.FramelessWindowHint
    color: "transparent"

    UkuiItems.Background {
        id: layoutBackGround

        anchors.fill: parent
        color: Theme.base()
        radius: 6
        clip: true
        Theme.onPaletteChanged: {
            color = Theme.base();
        }
        border.width: 1
        border.color: Qt.rgba(0, 0, 0, 0.1)

        CustomHeader {
            id: customHeader

            width: parent.width
            height: dragAreaHeight
            anchors.top: parent.top
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.leftMargin: 8
            anchors.rightMargin: 4
            headerTitle: qsTr("System Keyboard Layout")
            z: 1
            onDragStarted: {
                handleDragStarted(posX, posY);
            }
            onDragging: {
                handleDragging(posX, posY);
            }
            onDragEnded: {
                handleDragEnd();
            }
            onCloseClicked: {
                handleCloseClicked();
            }
        }

        ColumnLayout {
            anchors.top: customHeader.bottom
            anchors.topMargin: 16
            width: parent.width
            spacing: 16

            LayoutComboBox {
                id: languageComboBox

                width: parent.width
                height: itemHeight
                enableBackground: false
                labelText: qsTr("Language")
                texts: layoutManager.languageModel
                currentIndex: 0
                onComboBoxIndexChanged: (value, idx) => {
                    layoutManager.layoutModel.language = texts.language(value);
                    layoutComboBox.currentIndex = 0;
                    layoutManager.setVariantInfo(layoutManager.layoutModel.layoutInfo(layoutComboBox.currentIndex));
                    variantComboBox.currentIndex = 0;
                }
            }

            LayoutComboBox {
                id: layoutComboBox

                width: parent.width
                height: itemHeight
                enableBackground: false
                texts: layoutManager.layoutModel
                labelText: qsTr("Layout")
                currentIndex: 0
                onComboBoxIndexChanged: (value, idx) => {
                    if (currentIndex < 0)
                        return ;

                    layoutManager.setVariantInfo(texts.layoutInfo(currentIndex));
                    variantComboBox.currentIndex = 0;
                }
            }

            LayoutComboBox {
                id: variantComboBox

                width: parent.width
                height: itemHeight
                enableBackground: false
                texts: layoutManager.variantModel
                currentIndex: 0
                labelText: qsTr("Variant")
            }

            UkuiItems.Background {
                Layout.fillWidth: true
                height: 1
                Layout.leftMargin: layoutBackGround.border.width
                Layout.rightMargin: layoutBackGround.border.width
                Layout.preferredHeight: 1
                color: Theme.button()
                Theme.onPaletteChanged: {
                    color = Theme.button();
                }
            }

            RowLayout {
                Layout.fillWidth: true
                Layout.leftMargin: 24
                Layout.rightMargin: 24
                spacing: 8

                Item {
                    Layout.fillWidth: true
                }

                CustomButton {
                    width: 88
                    height: 36
                    labelText: qsTr("Cancel")
                    onCustomButtonClicked: cancelAction()
                }

                CustomButton {
                    width: 88
                    height: 36
                    enableHighlight: true
                    labelText: qsTr("OK")
                    Layout.alignment: Qt.AlignRight
                    onCustomButtonClicked: saveAction()
                }

            }

        }

    }

    Connections {
        function onLayoutChanged(currentLayout) {
            console.log("onLayoutChanged: " + currentLayout);
            var dashPos = currentLayout.indexOf("-");
            var layout, variant;
            if (dashPos >= 0) {
                variant = currentLayout.substring(dashPos + 1);
                layout = currentLayout.substring(0, dashPos);
            } else {
                layout = currentLayout;
            }
            languageComboBox.currentIndex = 0;
            layoutComboBox.currentIndex = layoutManager.layoutIndex(layout);
            if (variant === "")
                variantComboBox.currentIndex = 0;
            else
                variantComboBox.currentIndex = layoutManager.variantIndex(variant);
        }

        target: layoutManager
    }

}
